package com.eclink.asset.setup.controller;

import java.util.List;

import com.eclink.asset.sys.service.SysMenuServiceImpl;
import com.ruoyi.common.constant.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;

import javax.annotation.Resource;

/**
 * 菜单信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/menu")
public class SysMenuController extends BaseController
{
    @Resource
    private SysMenuServiceImpl menuService;

    /**
     * 列出当前用户的菜单
     * @return 列表
     */
    @GetMapping("/listMyMenu")
    public ResponseEntity<List<SysMenu>> listMyMenu() {
        List<SysMenu> menus = menuService.selectMyMenu();
        return ResponseEntity.ok(menus);
    }

    @GetMapping("/listTop")
    public ResponseEntity<List<SysMenu>> listTop() {
        List<SysMenu> menus = menuService.selectMenuListTop();
        return ResponseEntity.ok(menus);
    }

    @GetMapping("/listCommonMenu")
    public ResponseEntity<List<SysMenu>> getUserCommonMenu() {
        List<SysMenu> menus = menuService.getUserCommonMenu();
        return ResponseEntity.ok(menus);
    }

    /**
     * 获取菜单列表
     */
    @GetMapping("/list")
    public AjaxResult list()
    {
        List<SysMenu> menus = menuService.list();
        return AjaxResult.success(menus);
    }

    /**
     * 根据菜单编号获取详细信息
     */
    @GetMapping(value = "/{menuId}")
    public AjaxResult getInfo(@PathVariable Long menuId)
    {
        return AjaxResult.success(menuService.selectMenuById(menuId));
    }

    /**
     * 新增菜单
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysMenu menu)
    {

        menu.setCreateBy(getUserId());
        return toAjax(menuService.insertMenu(menu));
    }

    /**
     * 修改菜单
     */
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysMenu menu)
    {

        if (menu.getMenuId().equals(menu.getParentId()))
        {
            return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败，上级菜单不能选择自己");
        }
        menu.setUpdateBy(getUserId());
        return toAjax(menuService.updateMenu(menu));
    }

    /**
     * 删除菜单
     */
    @DeleteMapping("/{menuId}")
    public ResponseEntity<String> remove(@PathVariable("menuId") Long menuId)
    {
        if (menuService.hasChildByMenuId(menuId))
        {
            return ResponseEntity.status(HttpStatus.ERROR)
                    .body("存在子菜单,不允许删除");

        }
        menuService.deleteMenuById(menuId);
        return ResponseEntity.ok("操作成功");
    }
}
